#include "bulk_storage.h"
#include <stdio.h>

int main() {
  struct cacheStore * cs;
  int block, block2;
  int x = 42, i;
  int *y;
  /* long blockCount; */
  printf("Hello world! -- Lets do some testing.");

  cs = cs_open("cs_test", "cs_test.fat", (size_t)sizeof(int), (int)32, (off_t)100);

  block2 = cs_newBlock(cs);

  block = cs_newBlock(cs);

  x = -1;
  cs_writeBlock(cs, block);

  y = (int*)cs_readBlock(cs, block);
  x = *y;
  printf("Block %d contains %d Other block is %d\n", block, x, block2);
  cs_releaseBlock(cs, block);

  /*  blockCount = cs.blockCount; */

  cs_close(cs);

  /*  cs = cs_open("TEST_file_store", "TEST_file_store.fat", sizeof(int), 0);


  y = (int*)cs_readBlock(cs, block);
  x = *y;
  printf("Block %ld contains %d Other block is %d\n", block, x, block2);
  cs_releaseBlock(cs, block, y);

  cs_close(cs);
  */
  // exit(0);
  printf("That was fun, now some performance evaluations.\nCreate 1,000,000 int[30] blocks sequentially:\n");

  cs = cs_open("cs_big", "cs_big.fat", sizeof(int[30]), 128, 10000);

  for(i = 0; i < 1000000; i++) {
    int blockIdx = cs_newBlock(cs);
    int *block = cs_readBlock(cs, blockIdx);
    int j;
    debug(TRIVIAL, ("cs_newBlock:\t%d\t", blockIdx));


    for(j = 0; j < 30; j++) {
      block[j] = i;
    }
    cs_writeBlock(cs, blockIdx);
    debug(TRIVIAL, (" %lx\n", (intptr_t)cs_readBlock(cs, blockIdx)));
    if(i && ! (i % 100000)) {
      debug(INFO, ("  %d00,000\n", i/100000));
      cs_freeBlock(cs, blockIdx);
    }
  }

  printf("1,000,000\n");


  cs_close(cs);

  return 0;
}
